package com.example.addgesture;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    GestureOverlayView gestureView;
    EditText gestureName;
    private Gesture mGesture;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取手势编辑视图
        gestureView=(GestureOverlayView) this.findViewById(R.id.gesture);
        gestureView.setGestureColor(Color.BLUE);
        gestureView.setGestureStrokeWidth(10);
        //手势完成事件监听器
        gestureView.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) {
                mGesture=gesture;
                //加载save_gesture.xml界面布局视图
                View dialog=getLayoutInflater().inflate(R.layout.save_gesture,null);
                ImageView image=dialog.findViewById(R.id.show);
                gestureName=dialog.findViewById(R.id.gesture_name);
                //根据Gesture包含的手势创建一个位图
                Bitmap bitmap=gesture.toBitmap(128,128,10,0xff0000ff);
                image.setImageBitmap(bitmap);
                //使用对话框显示dialog组件
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setView(dialog);
                builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                    }
                });
                builder.setNegativeButton("取消",null);
                builder.create().show();
            }
        });
    }
    public void saveFile(){
        if(mGesture!=null){
            //获取指定文件对应的手势库
            GestureLibrary lib = GestureLibraries.fromFile("/sdcard/mygestures");
            lib.addGesture(gestureName.getText().toString(),mGesture);
            boolean result=lib.save();
            if(result)
                Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(this, "保存失败", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode==1){
            for(int i=0;i<permissions.length;i++){
                if(grantResults[i]== PackageManager.PERMISSION_GRANTED){
                    saveFile();
                }else{
                    Toast.makeText(this, ""+"权限"+permissions[i]+"申请失败，不能保存手势文件",
                            Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}
